package sg.edu.nus.iss.vmcs.machinery;

/*
 * Copyright 2011 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

import java.awt.*;
import java.awt.event.*;

import sg.edu.nus.iss.vmcs.store.*;

/**
 * This class instantiates the Machinery simulator panel which simulates the
 * physical actions of the maintainer. It enables the user to:
 * 1 display and enter new values for the number of cans of each {@link DrinksBrand} 
 * held by the {@link DrinksStore}.
 * 2 display and enter new values for the number of {@link Coin} of each denomination
 * held by the {@link CashStore}.
 * 3 display whether the vending machine {@link Door} is unlocked, and enable it to 
 * locked. This class inherits for JAVA AWT Dialog class.
 *
 * @version 1.0  07/12/2011
 * @author Team 7S - SE19
 */

public class MachinerySimulatorPanel extends Dialog {

	private static final String TITLE = "Machinery Panel";

	private StoreViewer cashDisplay;
	private StoreViewer drinksDisplay;
	private Checkbox doorDisplay;
	private StoreController storeCtrl;
	private MachineryController machineryCtrl;
	
	StoreItem cashStoreItem[] ;
	StoreItem drinkStoreItem[];

/**
 * This constructor creates an instance of the Machinery Simulator Panel. It further
 * creates Drinks Store Display, Cash Store Display and Door Status DIsplay. It
 * receives and keeps the reference for Store Controller
 * @param fr
 * @param machCtrl
 * @see MachineryController
 */
	public MachinerySimulatorPanel(Frame fr, MachineryController machCtrl) {
		super(fr, TITLE, false);

		machineryCtrl = machCtrl;
		storeCtrl = machineryCtrl.getMainController().getStoreController();

		Label lb = new Label(TITLE);
		lb.setFont(new Font("Helvetica", Font.BOLD, 24));
		lb.setAlignment(Label.CENTER);

		cashDisplay = new StoreViewer(Store.CASH, storeCtrl);
		drinksDisplay = new StoreViewer(Store.DRINK, storeCtrl);

		Panel tp = new Panel();
		tp.setLayout(new GridLayout(0, 1));
		tp.add(cashDisplay);
		tp.add(drinksDisplay);

		Panel dp = new Panel();
		doorDisplay = new Checkbox();
		doorDisplay.addItemListener(new DoorListener(machineryCtrl));
		doorDisplay.setLabel("Door Locked");
		dp.add(doorDisplay);

		this.setLayout(new BorderLayout());
		this.add("North", lb);
		this.add("Center", tp);
		this.add("South", dp);
		pack();

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				machineryCtrl.closeMachineryPanel();
			}
		});
		
		registerObserver();
	}

//ADDED FOR OBSERVER PATTERN
   public void registerObserver(){
		
		cashStoreItem = storeCtrl.getStoreItems(Store.CASH);
		drinkStoreItem = storeCtrl.getStoreItems(Store.DRINK);
		if(cashStoreItem!=null)for(int i = 0; i < cashStoreItem.length;i++){
			cashStoreItem[i].addObserver(cashDisplay);
		}
		if(drinkStoreItem!=null)for(int i = 0; i < drinkStoreItem.length;i++){
			drinkStoreItem[i].addObserver(drinksDisplay);
		}
	}
 
/**
 * This method display the Machinery Simulator Panel. This will be achieved by
 * displaying the frame of the panel and then sending messages to its constituent
 * objects instructing them to:
 * 1 display themselves, 2 set initial value and 3 deactivate themselves.
 */
	public void display() {
		this.setVisible(true);
	}

/**
 * Remove the panel objects and Machinery Simulator Panel from the display.
 */
	public void closeDown() {
		dispose();
	}

/**
 * Retrieves the Cash Store Display object
 * @return cashDisplay retrieves Store Viewer Object
 * @see StoreViewer
 */
	public StoreViewer getCashStoreDisplay() {
		return cashDisplay;
	}

/**
 * Retrieves the Drinks Store Display object
 * @return drinkDisplay retrieves Store Viewer Object
 * @see StoreViewer
 */
	public StoreViewer getDrinksStoreDisplay() {
		return drinksDisplay;
	}

/**
 * This methods set state of the Door object
 * @param state Either Open or Close
 */	
	public void setDoorState(boolean state) {
		doorDisplay.setState(state);
		this.setActive(!state);

	}

/**
 * This operation activates or deactivates the Machinery Simulator Panel and its
 * component objects
 * @param state This state is active or deactive (true or false)
 * @see StoreViewer
 */	
	public void setActive(boolean state) {
		cashDisplay.setActive(state);
		drinksDisplay.setActive(state);
		doorDisplay.setEnabled(state);
	}

}
